package edu.gatech.volt2.drdoommgt.presenter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.List;

import javax.swing.JOptionPane;

import edu.gatech.volt2.drdoommgt.domain.*;
import edu.gatech.volt2.drdoommgt.gui.AppointmentRequestWindow;
import edu.gatech.volt2.drdoommgt.gui.IAppointmentView;
import edu.gatech.volt2.drdoommgt.system.DataBaseAccessor;

public class AppointmentPresenter implements ActionListener{
	
	private String patientName;
	private Appointment myAppointment;
	private IAppointmentView myView; 

	public AppointmentPresenter(Appointment myAppointment, IAppointmentView view){
		this.myAppointment= myAppointment;
		this.myView= view;
		myView.addListener(this);
		
		DataBaseAccessor db = new DataBaseAccessor();
		db.connectToDatabase();
		List<Doctor> docList = db.getAllDoctors();
		myView.setDoctorsList(docList);
		
		this.setAppointmentInformation();
		
		myView.showSelf();
	}
	
	public AppointmentPresenter(Appointment myAppointment, IAppointmentView view, String patientName){
		this.myAppointment= myAppointment;
		this.myView= view;
		this.patientName= patientName;
		myView.addListener(this);
		
		DataBaseAccessor db = new DataBaseAccessor();
		db.connectToDatabase();
		List<Doctor> docList = db.getAllDoctors();
		myView.setDoctorsList(docList);
		
		this.setAppointmentInformation();
		
		myView.showSelf();
	}

	public AppointmentPresenter(DataBaseAccessor dba, Patient myPatient) {
		this.myAppointment= new Appointment();
		this.myView= new AppointmentRequestWindow();
		this.patientName= myPatient.getName().toString();
		myView.addListener(this);
		
		DataBaseAccessor db = new DataBaseAccessor();
		db.connectToDatabase();
		List<Doctor> docList = db.getAllDoctors();
		myView.setDoctorsList(docList);
		
		this.setAppointmentInformation();
		
		myView.showSelf();
	}

	private void setAppointmentInformation() {
		if ( this.myAppointment != null ){
		}
	}

	public void actionPerformed(ActionEvent action) {
		if ( action.getSource() == myView.getCancelButton()){
			System.out.println("Exiting...");
			myView.hideSelf();
		}else{	
			String timeHour = myView.getWhenTime().getSelectedItem().toString();
			int hour = 0, minute = 0;
			if ( timeHour.equals("Any Time")){
				hour = 0;
				minute = 0;
			}
			else{
				String[] timeParts = timeHour.split(":");
				if ( timeParts.length == 2){
					hour = Integer.parseInt(timeParts[0].toString());
					if ( timeParts[1].contains("pm")){
						hour+=12;
					}
					String[] minuteParts = timeParts[1].split(" ");
					minute = Integer.parseInt(minuteParts[0]);
				}
			}
			
			Calendar cal = Calendar.getInstance();
			int year = Integer.parseInt(myView.getWhenYear().getSelectedItem().toString());
			int month = Integer.parseInt(myView.getWhenMonth().getSelectedItem().toString());
			int date = Integer.parseInt(myView.getWhenDay().getSelectedItem().toString());
			cal.set(year, month, date, hour, minute);

			DataBaseAccessor db = new DataBaseAccessor();
			db.connectToDatabase();
			Doctor requestedDoc = db.find(Doctor.class, ((Doctor)myView.getDoctorName().getSelectedItem()).getId());
			Appointment newApp = new Appointment();
			newApp.setReason(myView.getReasonText().getText());
			newApp.setRequestedDate(cal);
			newApp.setRequestedDoc(requestedDoc);
			newApp.setPatientName(this.patientName);
			
			db.save(newApp);
			db.closeConnectionToDatabase();

			if ( newApp.getId() > 0 ){	/// Check to insure the appointment has been submitted. 
				JOptionPane.showMessageDialog(null, "Appointment Request Submitted");
			}else{
				JOptionPane.showMessageDialog(null, "Appointment Request Could Not be Submitted");
			}
			
			myView.hideSelf();
		}
	}

}
